# -*- coding: utf-8 -*-

import wx
import wizdata
import  wx.wizard as wiz


class ExistValidator(wx.PyValidator):
    def __init__(self):
        wx.PyValidator.__init__(self)
        #self.Bind(wx.EVT_CHAR, self.OnChar)
        
        
    def Clone(self):
        """
        Note that every validator must implement the Clone() method.
        """
        return ExistValidator()
        
    def Validate(self, win):
        obj = self.GetWindow()
        data = wizdata.WizData()
        text = obj.GetValue()
        list = win.getMapping()
        #print self.GetPreviousHandler()
        #print text
        #print data.list 
        if text.strip() == "" and data.dir == 1 :
            wx.MessageBox(u"请选项唯一标识", u"提示")
            obj.SetBackgroundColour("#D8BFD8")
            obj.SetFocus()
            obj.Refresh()
            return False
        elif list.count(text) == 0 and data.dir == 1 :
            wx.MessageBox(u"没有唯一标识\n改变唯一标识域或改变映射域", u"提示")
            obj.SetBackgroundColour("#D8BFD8")
            obj.SetFocus()
            obj.Refresh()
            return False
        else:
            data.list = list
            data.key_col = text #.encode('utf8')
            obj.Refresh()
            #textCtrl.SetValue("ok")
            return True
        
    def TransferToWindow(self):
        return True
        
    def TransferFromWindow(self):
        return True

class CharValidator(wx.PyValidator):
    def __init__(self):
        wx.PyValidator.__init__(self)
        #self.Bind(wx.EVT_CHAR, self.OnChar)
        
        
    def Clone(self):
        """
        Note that every validator must implement the Clone() method.
        """
        return CharValidator()
        
    def Validate(self, win):
        obj = self.GetWindow()
        data = wizdata.WizData()
        text = obj.GetValue()
        #print self.GetPreviousHandler()
        if text.strip() == "" and data.dir == 1 :
            wx.MessageBox(u"文本框不能为空", u"提示")
            obj.SetBackgroundColour("#D8BFD8")
            obj.SetFocus()
            obj.Refresh()
            return False
        elif data.dir == -1:
            return True
        else:
            obj.Refresh()
            #textCtrl.SetValue("ok")
            return True
        
    def TransferToWindow(self):
        return True
        
    def TransferFromWindow(self):
        return True  
    
    def OnChar(self,event):
        #print "on char"
        event.Skip()



